package com.daily.videoedit.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;

import androidx.appcompat.app.AppCompatActivity;

import com.daily.videoedit.R;
import com.daily.videoedit.utils.FFmpegUtil;
import com.daily.videoedit.utils.GlideEngine;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;
import com.mobile.ffmpeg.util.FFmpegAsyncUtils;
import com.mobile.ffmpeg.util.FFmpegExecuteCallback;

import java.io.File;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final int CODE_REMOVE_STICKER = 1;
    private static final int CODE_CUTE_RATIO = 2;
    private static final int CODE_CUTE = 3;
    private static final int CODE_ADD_MUSIC = 4;
    private static final int CODE_IN_PIC = 5;
    private static final int CODE_VIDEO_SPLIT = 6;
    private static final String TAG = MainActivity.class.getSimpleName();
    private ProgressBar mProgressDialog;

    private String destPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera/FFmpeg/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        File file = new File(destPath);
        if (!file.exists()){
            file.mkdirs();
        }
        mProgressDialog = findViewById(R.id.progress_bar);
        findViewById(R.id.remove_sticker_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectVideo(CODE_REMOVE_STICKER);
            }
        });
        findViewById(R.id.cancas_scale_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectVideo(CODE_CUTE_RATIO);
            }
        });
        findViewById(R.id.video_cut_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectVideo(CODE_CUTE);
            }
        });

        findViewById(R.id.add_music_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectVideo(CODE_ADD_MUSIC);
            }
        });

        findViewById(R.id.in_pic_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectVideo(CODE_IN_PIC);
            }
        });
        findViewById(R.id.video_split_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectVideo(CODE_VIDEO_SPLIT);
            }
        });
    }

    private void selectVideo(int requestCode){
        PictureSelector.create(this)
                .openGallery(PictureMimeType.ofVideo())
                .loadImageEngine(GlideEngine.createGlideEngine()) // Please refer to the Demo GlideEngine.java
                .forResult(requestCode);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
            String videoPath = selectList.get(0).getPath();
            switch (requestCode) {
                case CODE_REMOVE_STICKER:
                    // onResult Callback
                    removeSticker(videoPath);
                    break;
                case CODE_CUTE_RATIO:
                    cuteRatioVideo(videoPath);
                    break;
                case CODE_CUTE:
                    cuteVideo(videoPath);
                    break;
                case CODE_ADD_MUSIC:
                    addMusic(videoPath);
                    break;
                case CODE_IN_PIC:
                    inPicVideo(videoPath);
                    break;
                case CODE_VIDEO_SPLIT:
                    videoSplit(videoPath);
                    break;
                default:
                    break;
            }
        }
    }

    private void videoSplit(String videoPath) {
        FFmpegAsyncUtils fmpegAsyncUtils = new FFmpegAsyncUtils();
        fmpegAsyncUtils.setCallback(new FFmpegExecuteCallback() {
            @Override
            public void onFFmpegStart() {
                mProgressDialog.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFFmpegSucceed(String executeOutput) {
                mProgressDialog.setVisibility(View.GONE);
            }

            @Override
            public void onFFmpegFailed(String executeOutput) {
                Log.e(TAG, "onFFmpegFailed:"+executeOutput);
                mProgressDialog.setVisibility(View.GONE);
            }

            @Override
            public void onFFmpegProgress(Integer progress) {
                Log.e(TAG, "onFFmpegProgress:"+progress);
            }

            @Override
            public void onFFmpegCancel() {
                Log.e(TAG, "onFFmpegCancel:");
            }
        });

        String mixFilePath = destPath+"1608624039494.mp4";
        String targetFilePath = destPath+System.currentTimeMillis()+".mp4";
        String[] cmd = FFmpegUtil.multVideo(videoPath, mixFilePath, targetFilePath);
        fmpegAsyncUtils.execute(cmd);
    }

    private void inPicVideo(String videoPath) {
        FFmpegAsyncUtils fmpegAsyncUtils = new FFmpegAsyncUtils();
        fmpegAsyncUtils.setCallback(new FFmpegExecuteCallback() {
            @Override
            public void onFFmpegStart() {
                mProgressDialog.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFFmpegSucceed(String executeOutput) {
                mProgressDialog.setVisibility(View.GONE);
            }

            @Override
            public void onFFmpegFailed(String executeOutput) {
                Log.e(TAG, "onFFmpegFailed:"+executeOutput);
                mProgressDialog.setVisibility(View.GONE);
            }

            @Override
            public void onFFmpegProgress(Integer progress) {

            }

            @Override
            public void onFFmpegCancel() {

            }
        });

        String mixFilePath = destPath+"temp.mp4";
        String targetFilePath = destPath+System.currentTimeMillis()+".mp4";
        String[] cmd = FFmpegUtil.picInPicVideo(videoPath, mixFilePath, 0, 250, targetFilePath);
        fmpegAsyncUtils.execute(cmd);
    }

    private void addMusic(String videoPath) {
        FFmpegAsyncUtils fmpegAsyncUtils = new FFmpegAsyncUtils();
        fmpegAsyncUtils.setCallback(new FFmpegExecuteCallback() {
            @Override
            public void onFFmpegStart() {
                mProgressDialog.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFFmpegSucceed(String executeOutput) {
                mProgressDialog.setVisibility(View.GONE);
            }

            @Override
            public void onFFmpegFailed(String executeOutput) {
                Log.e(TAG, "onFFmpegFailed:"+executeOutput);
                mProgressDialog.setVisibility(View.GONE);
            }

            @Override
            public void onFFmpegProgress(Integer progress) {

            }

            @Override
            public void onFFmpegCancel() {

            }
        });

        String mixFilePath = destPath+"bgmAudio.mp3";
        String targetFilePath = destPath+System.currentTimeMillis()+".mp4";
        String[] cmd = FFmpegUtil.mixAudio(videoPath, mixFilePath, targetFilePath);
        fmpegAsyncUtils.execute(cmd);
    }

    /**
     * Desc:视频按比例裁剪
     * <p>
     * Author: [李豫]
     * Date: 2020-12-22
     *
     * @param path
     */
    private void cuteVideo(String path) {
        FFmpegAsyncUtils fmpegAsyncUtils = new FFmpegAsyncUtils();
        fmpegAsyncUtils.setCallback(new FFmpegExecuteCallback() {
            @Override
            public void onFFmpegStart() {
                mProgressDialog.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFFmpegSucceed(String executeOutput) {
                mProgressDialog.setVisibility(View.GONE);
            }

            @Override
            public void onFFmpegFailed(String executeOutput) {
                Log.e(TAG, "onFFmpegFailed:"+executeOutput);
                mProgressDialog.setVisibility(View.GONE);
            }

            @Override
            public void onFFmpegProgress(Integer progress) {

            }

            @Override
            public void onFFmpegCancel() {

            }
        });

        String targetFilePath = destPath+System.currentTimeMillis()+".mp4";
        String[] cmd = FFmpegUtil.cutVideo(path, "10", "22", targetFilePath);
        fmpegAsyncUtils.execute(cmd);
    }

    /**
     * Desc:视频按比例裁剪
     * <p>
     * Author: [李豫]
     * Date: 2020-12-22
     *
     * @param path
     */
    private void cuteRatioVideo(String path) {
        FFmpegAsyncUtils fmpegAsyncUtils = new FFmpegAsyncUtils();
        fmpegAsyncUtils.setCallback(new FFmpegExecuteCallback() {
            @Override
            public void onFFmpegStart() {
                mProgressDialog.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFFmpegSucceed(String executeOutput) {
                mProgressDialog.setVisibility(View.GONE);
            }

            @Override
            public void onFFmpegFailed(String executeOutput) {
                Log.e(TAG, "onFFmpegFailed:"+executeOutput);
                mProgressDialog.setVisibility(View.GONE);
            }

            @Override
            public void onFFmpegProgress(Integer progress) {

            }

            @Override
            public void onFFmpegCancel() {

            }
        });

        String targetFilePath = destPath+System.currentTimeMillis()+".mp4";
        String[] cmd = FFmpegUtil.cutVideoRatio(path, 500, 500, 100, 100, targetFilePath);
        fmpegAsyncUtils.execute(cmd);
    }

    /**
     * Desc:去水印
     * <p>
     * Author: [李豫]
     * Date: 2020-12-22
     *
     * @param path
     */
    private void removeSticker(String path) {
        FFmpegAsyncUtils fmpegAsyncUtils = new FFmpegAsyncUtils();
        fmpegAsyncUtils.setCallback(new FFmpegExecuteCallback() {
            @Override
            public void onFFmpegStart() {
                mProgressDialog.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFFmpegSucceed(String executeOutput) {
                mProgressDialog.setVisibility(View.GONE);
            }

            @Override
            public void onFFmpegFailed(String executeOutput) {
                mProgressDialog.setVisibility(View.GONE);
                Log.e(TAG, "onFFmpegFailed:"+executeOutput);
            }

            @Override
            public void onFFmpegProgress(Integer progress) {

            }

            @Override
            public void onFFmpegCancel() {

            }
        });

        String targetFilePath = destPath+System.currentTimeMillis()+".mp4";
        String[] cmd = FFmpegUtil.removeWaterMark(path, 10, 10, 200, 200, targetFilePath);
        fmpegAsyncUtils.execute(cmd);
    }
}